<?php
require_once dirname(dirname(__FILE__)).'/abstract.php';

class SF_Catalog_Category_AutoUpdateCategoryStatus extends Mage_Shell_Abstract
{
    public function autoUpdate()
	{
		Mage::log('自动开启分类触发开始',1,'rumall.log');
        $categoryCollection = Mage::getModel('catalog/category')->getCollection();
        foreach ($categoryCollection as $category) {
			$prodCollection = Mage::getResourceModel('catalog/product_collection')
								->addAttributeToSelect('*')
								->addCategoryFilter($category)
								->addAttributeToFilter('status', 1)
								->addAttributeToFilter('visibility', 4)
								->addAttributeToFilter('approval', 2);
			if(count($prodCollection)>0){
				$_category = Mage::getModel('catalog/category')->setStoreId(0)->load($category->getId());
				if($_category->getIsActive() == '0'){
					$_category->setIsActive('1');
					$_category->save();
					Mage::log('已开启的分类:'.$_category->getName()
						.', ID是:'.$_category->getId()
						.',	视图是:0',
						1,'rumall.log'
					);
				}
				$_category = Mage::getModel('catalog/category')->setStoreId(2)->load($category->getId());
				if($_category->getIsActive() == '0'){
					$_category->setIsActive('1');
					$_category->save();
					Mage::log('已开启的分类:'.$_category->getName()
						.', ID是:'.$_category->getId()
						.',	视图是:2',
						1,'rumall.log'
					);
				}
			}else{
				$_category = Mage::getModel('catalog/category')->setStoreId(0)->load($category->getId());
				if($_category->getIsActive() == '1'){
					$_category->setIsActive('0');
					$_category->save();
					Mage::log('已关闭的分类:'.$_category->getName()
						.', ID是:'.$_category->getId()
						.',	视图是:0',
						1,'rumall.log'
					);
				}
				$_category = Mage::getModel('catalog/category')->setStoreId(2)->load($category->getId());
				if($_category->getIsActive() == '1'){
					$_category->setIsActive('0');
					$_category->save();
					Mage::log('已关闭的分类:'.$_category->getName()
						.', ID是:'.$_category->getId()
						.',	视图是:2',
						1,'rumall.log'
					);
				}
			}
        }
		Mage::log('自动开启分类触发结束',1,'rumall.log');
	}

    public function run()
	{
		$this->autoUpdate();
	}
}


$C = new SF_Catalog_Category_AutoUpdateCategoryStatus();
$C->run();
     